package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostItemSubscriptionDao;
import ysq.xyg.host.model.po.HostItemSubscription;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostItemSubscriptionVo;
import ysq.xyg.host.service.HostItemSubscriptionService;

import java.util.Date;
import java.util.List;

/**
 * 房产模块-动态订阅service实现
 *
 * @author congcong tao
 * @date 2020-3-6 10:12:54
 */
@Service("hostItemSubscriptionService")
public class HostItemSubscriptionServiceImpl extends BaseServiceImpl<HostItemSubscription> implements HostItemSubscriptionService {

    @Autowired
    private HostItemSubscriptionDao hostItemSubscriptionDao;

    @Autowired
    public void setBaseDao(HostItemSubscriptionDao hostItemSubscriptionDao) {
        this.baseDao = hostItemSubscriptionDao;
    }

    /**
     * 分页查询
     *
     * @param hostItemSubscriptionVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-3-6 10:12:54
     */
    @Override
    public ReturnEntity page(HostItemSubscriptionVo hostItemSubscriptionVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" create_time desc");
        List<HostItemSubscription> list = hostItemSubscriptionDao.selectEntityList(hostItemSubscriptionVo);
        PageInfo<HostItemSubscription> page = new PageInfo<HostItemSubscription>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostItemSubscriptionVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-3-6 10:12:54
     */
    @Override
    public ReturnEntity addOrUpdate(HostItemSubscriptionVo hostItemSubscriptionVo, SysUser currentUser) {
        boolean isUpate = true;
        HostItemSubscription hostItemSubscription;
        if (StrUtil.isEmpty(hostItemSubscriptionVo.getId())) {
            hostItemSubscription = new HostItemSubscription(currentUser);
            isUpate = false;
        } else {
            hostItemSubscription = selectByPrimaryKey(hostItemSubscriptionVo.getId());
            if (ObjectUtil.isNull(hostItemSubscription)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostItemSubscription.setUpdateTime(new Date());
        }
        hostItemSubscription.setAccountId(hostItemSubscriptionVo.getAccountId());
        hostItemSubscription.setMemberId(hostItemSubscriptionVo.getMemberId());
        hostItemSubscription.setItemId(hostItemSubscriptionVo.getItemId());
        hostItemSubscription.setStatus(hostItemSubscriptionVo.getStatus());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostItemSubscription);
        } else {
            num = insert(hostItemSubscription);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2020-3-6 10:12:54
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItemSubscription hostItemSubscription = selectByPrimaryKey(id);
        if (hostItemSubscription != null) {
            hostItemSubscription.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItemSubscription.setUpdateTime(new Date());
            int num = updateByPrimaryKey(hostItemSubscription);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemSubscriptionVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2020-3-6 10:12:54
     */
    @Override
    public List<HostItemSubscription> acceptList(HostItemSubscriptionVo hostItemSubscriptionVo){
        Example example = new Example(HostItemSubscription.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostItemSubscriptionVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostItemSubscriptionVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostItemSubscriptionVo.getItemId())) {
            criteria.andEqualTo("itemId", hostItemSubscriptionVo.getItemId());
        }
        if (StringUtil.isNotEmpty(hostItemSubscriptionVo.getMemberId())) {
            criteria.andEqualTo("memberId", hostItemSubscriptionVo.getMemberId());
        }
        example.setOrderByClause(" create_time desc");
        return hostItemSubscriptionDao.selectByExample(example);
    }
}
